// pages/Purchasing/Purchasing.js
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sstylea: 'menu-m-select',
    sstyleb: '',
    sstylec: '',
    MCinmateGroup: [{ name: '澳門居民', value: 'MC1', checked: true }, { name: '非澳門居民', value: 'MC2' }],
    PurchasingGroup: [{ name: '首次置業', value: 'first', checked: true }, { name: '非首次置業', value: 'nofirst' }],
    RealHouseGroup: [{ name: '現樓', value: 'Real', checked: true }, { name: '樓花', value: 'Stage' }],
    MoyouthGroup: [{ name: '首置青年(21-44歲)', value: 'youth', checked: false }],
    MCinmateshow: false,
    Moyouthshow:false,
    MCinmate: 'MC1',
    Purchasing: 'first',
    Moyouth:'',
    calculateType:'Real',
    installment: '',//每月供款能力
    firstphase: '',//首期
    annualrate: '',//年利率
    repaymentyear: '',//還款年期
    monthlytab: true,
    firstphasetab: false ,
    pricetab: true,
    firstphaseText: '',
    firstphaserate: '',
    firstphaseyear: '',
    firstphaseMCinmateshow: false,
    firstMoyouthshow:false,
    firstMCinmate: 'MC1',
    firstPurchasing: 'first',
    firstMoyouth: 'youth',
    firstcalculateType: 'Real',
    houseMCinmateshow: false,
    houseMoyouthshow:false,
    houseMCinmate: 'MC1',
    housePurchasing: 'first',
    housepriceyear: '',
    housepricerate: '',
    housepriceText: '',
    housecalculateType: 'Real',
    houseMoyouth: '',
    animationData: '',
    current: 0,
    _epyear: true,
    _eprate: true,
    _efrate: true,
    _efyear: true,
    _emrate: true,
    _emyear: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.removeStorageSync('kPurchasing')
    wx.removeStorageSync('kMCinmate')
    wx.removeStorageSync('kinstallment')
    wx.removeStorageSync('kfirstphase')
    wx.removeStorageSync('kannualrate')
    wx.removeStorageSync('krepaymentyear')
    wx.removeStorageSync('kfirstphaseText')
    wx.removeStorageSync('kfirstphaserate')
    wx.removeStorageSync('kfirstphaseyear')
    wx.removeStorageSync('kfirstMCinmate')
    wx.removeStorageSync('kfirstPurchasing')
    wx.removeStorageSync('kfirstMoyouth') 
    wx.removeStorageSync('kfirstcalculateType')  
  
  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  clickTap: function (e) {
    console.log(e.target.dataset.key)
    var key = e.target.dataset.key
    this.overTab(key)

  },
  SelectTap: function (e) {

    var key = e.detail.current
    this.overTab(key)

  }
  ,
  overTab: function (key) {
    if (key == '0') {
      this.setData({
        monthlytab: true,
        firstphasetab: false ,
        pricetab: true,
        sstylea: 'menu-m-select',
        sstyleb: '',
        sstylec: '',
        current: 0,
      })

    }
    if (key == '1') {
      this.setData({
        monthlytab: false,
        firstphasetab: true,
        pricetab: true,
        sstylea: '',
        sstyleb: 'menu-m-select',
        sstylec: '',
        current: 1
      })
    }
    if (key == '2') {
      this.setData({
        monthlytab: true,
        firstphasetab: true,
        pricetab: false,
        sstylea: '',
        sstyleb: '',
        sstylec: 'menu-m-select',
        current: 2
      })
    }
  },


  InstallmentInput: function (e) {

    this.setData({
      installment: e.detail.value
    })
  },
  FirstphaseInput: function (e) {
    this.setData({
      firstphase: e.detail.value
    })
  },
  AnnualrateInput: function (e) {
    var valu = parseFloat(e.detail.value)
    if (isNaN(valu) || valu < 0 || valu >= 100) {
   this.setData({ _emrate: false })
      return '';
    }
    var srate = valu.toString().split(".")[1];
    if (srate != undefined) {
      if (srate.length > 3) {
        return util.Numberdigits(valu, 3)
      }
    }
    this.setData({
      annualrate: e.detail.value,
      _emrate: true,
    })
  },
  RepaymentInput: function (e) {
    var valu = parseFloat(e.detail.value)
    if (isNaN(valu) || valu < 1 || valu > 50) {
      this.setData({ _emyear: false })

      return '';
    }
    this.setData({
      repaymentyear: e.detail.value,
      _emyear: true,
    })
  },
  MoyouthChange:function(e){
  
    if (e.detail.value.length>0)
    {
    this.setData({
      Moyouth: e.detail.value
    })
    }else{
      this.setData({
        Moyouth: ''
      })
    }



 
  },
  MCinmateGroupChange: function (e) {

    if (e.detail.value == 'MC1') {
      this.setData({
        MCinmateshow: false,
        MCinmate: e.detail.value,
        Moyouthshow:false,
       
      })
    }
    else {
      this.setData({
        MCinmateshow: true,
        MCinmate: e.detail.value,
       // Purchasing: '',
        Moyouthshow:true
      })
    }
  },
  RealHouseChange:function(e){
     
    this.setData({
      calculateType: e.detail.value
    })
  },
  firstMoyouthChange:function(e){
    
    this.setData({

      firstMoyouth: e.detail.value
    })
  },
  PurchasingGroupChange: function (e) {
   
    if (e.detail.value == 'first') {
      this.setData({
        Moyouthshow: false
      })
    }
    else{
      this.setData({
        Moyouthshow: true

      })
    }
     
    this.setData({
      Purchasing: e.detail.value

    })
  },
  
  calculateMonthTap: function (e) {//|| this.data.firstphase.length <= 0
    if (this.data.installment.length <= 0  || this.data.annualrate.length <= 0 || this.data.repaymentyear.length <= 0) {
      wx.showModal({
        title: '提示',
        content: '請填寫完整的計算數據！',
        showCancel: false
      })
      return false;
    }
    if (parseFloat(this.data.annualrate) <= 0.001) {
      this.setData({ _emrate: false })
      return false;
    }
    
    wx.setStorageSync('kPurchasing', this.data.Purchasing)
    wx.setStorageSync('kMCinmate', this.data.MCinmate)
    wx.setStorageSync('kinstallment', this.data.installment)
    wx.setStorageSync('kfirstphase', this.data.firstphase)
    wx.setStorageSync('kannualrate', this.data.annualrate)
    wx.setStorageSync('krepaymentyear', this.data.repaymentyear)
    wx.setStorageSync('krepaymentyear', this.data.repaymentyear)
    wx.setStorageSync('kfirstMoyouth', this.data.Moyouth)
   

    wx.setStorageSync('kfirstcalculateType', this.data.calculateType) 
    wx.navigateTo({
      url: '/pages/Purchasing/Monthly/Monthly?Type=monthly',

    })
  },
  firstphaseTextInput: function (e) {
    // var num = util.replaceNum(e.detail.value)
    this.setData({
      firstphaseText: e.detail.value
    })

  },

  firstphaserateInput: function (e) {
    var valu = parseFloat(e.detail.value)
    if (isNaN(valu) || valu < 0 || valu >= 100) {
    
      this.setData({ _efrate: false })
      return '';
    }
    var srate = valu.toString().split(".")[1];
    if (srate != undefined) {
      if (srate.length > 3) {
        return util.Numberdigits(valu, 3)
      }
    }
    this.setData({
      firstphaserate: e.detail.value,
      _efrate: true
    })
  },
  firstphaseyearInput: function (e) {
    var valu = parseFloat(e.detail.value)
    if (isNaN(valu) || valu < 1 || valu > 50) {
      /*  wx.showModal({
          title: '提示',
          content: '还款年限不可高於40年，且小於1年',
          showCancel: false
        })*/
      this.setData({ _efyear: false })
      return '';
    }

    this.setData({
      firstphaseyear: e.detail.value,
      _efyear: true
    })

  },
  firstRealHouseChange:function(e){
    this.setData({
      firstcalculateType: e.detail.value
    })
  },

  firstMCinmateChange: function (e) {
    if (e.detail.value == 'MC1') {
      this.setData({
        firstphaseMCinmateshow: false,
        firstMCinmate: e.detail.value,
        firstMoyouthshow:false,
      })
    }
    else {
      this.setData({
        firstphaseMCinmateshow: true,
        firstMCinmate: e.detail.value,
        firstMoyouthshow: true,
      })
    }
  },
  firstPurchasingChange: function (e) {
    if (e.detail.value == 'first') {
      this.setData({
        firstMoyouthshow: false
      })
    }
    else {
      this.setData({
        firstMoyouthshow: true
      })
    }
    this.setData({
      firstPurchasing: e.detail.value

    })
  },
  //清除數據
  clean2Tap: function (e) {
    this.setData({
      firstphaseText: '',
      firstphaserate: '',
      firstphaseyear: '',
    })
  },

  calculatefirstphaseTap: function (e) {
    if (this.data.firstphaseText.length <= 0 || this.data.firstphaserate.length <= 0 || this.data.firstphaseyear.length <= 0) {
      wx.showModal({
        title: '提示',
        content: '請填寫完整的計算數據！',
        showCancel: false
      })
      return false;
    }
    if (parseFloat(this.data.firstphaserate) <= 0.001) {
      this.setData({ _efrate: false })
      return false;
    }
    wx.setStorageSync('kfirstphaseText', this.data.firstphaseText)
    wx.setStorageSync('kfirstphaserate', this.data.firstphaserate)
    wx.setStorageSync('kfirstphaseyear', this.data.firstphaseyear)
    wx.setStorageSync('kfirstMCinmate', this.data.firstMCinmate)
    wx.setStorageSync('kfirstPurchasing', this.data.firstPurchasing)
    wx.setStorageSync('kfirstMoyouth', this.data.firstMoyouth)
    wx.setStorageSync('kfirstcalculateType', this.data.firstcalculateType) 

     

    wx.navigateTo({
      url: '/pages/Purchasing/firstphase/firstphase',
    })
  },
  /*按樓價計算*/
  housepriceChange: function (e) {
    if (e.detail.value == 'MC1') {
      this.setData({
        houseMCinmateshow: false,
        houseMCinmate: e.detail.value,
        houseMoyouthshow:false,
      })
    }
    else {
      this.setData({
        houseMCinmateshow: true,
        houseMCinmate: e.detail.value,
        houseMoyouthshow:true,
      })
    }
  },
  houseRealHouseChange:function(e){
    this.setData({
      housecalculateType: e.detail.value
    })

  },
  housePurchasingChange: function (e) {
    if (e.detail.value =='first')
    {
      this.setData({
        houseMoyouthshow:false
      })
    }
    else{
      this.setData({
        houseMoyouthshow: true
      })
    }

    this.setData({
      housePurchasing: e.detail.value
    })
  },
  houseMoyouthChange:function(e){
    this.setData({
      houseMoyouth: e.detail.value
    })
  },
  clean3Tap: function (e) {
    this.setData({
      housepriceyear: '',
      housepricerate: '',
      housepriceText: ''
    })
  },
  housepriceTextInput: function (e) {
    this.setData({
      housepriceText: e.detail.value
    })
  },


  housepricerateInput: function (e) {
    var valu = parseFloat(e.detail.value)
    if (isNaN(valu) || valu < 0 || valu >= 100) {
      /*  wx.showModal({
          title: '提示',
          content: '年利率只允许输入0~100间的数字',
          showCancel: false
        })*/
      this.setData({ _eprate: false })
      return '';
    }
    var srate = valu.toString().split(".")[1];
    if (srate != undefined) {
      if (srate.length > 3) {
        return util.Numberdigits(valu, 3)
      }
    }
    this.setData({
      housepricerate: e.detail.value,
      _eprate: true
    })
  },
  housepriceyearInput: function (e) {
    var valu = parseFloat(e.detail.value)
    if (isNaN(valu) || valu < 1 || valu > 50) {
      /*  wx.showModal({
          title: '提示',
          content: '还款年限不可高於40年，且小於1年',
          showCancel: false,
        })*/
      this.setData({ _epyear: false })
      return ''
    }
    this.setData({
      housepriceyear: e.detail.value,
      _epyear: true
    })
  },
  calculatehousepriceTap: function () {
    if (this.data.housepriceText.length <= 0 || this.data.housepricerate.length <= 0 || this.data.housepriceyear.length <= 0) {
      wx.showModal({
        title: '提示',
        content: '請填寫完整的計算數據！',
        showCancel: false
      })
      return false;
    }
    if (parseFloat(this.data.housepricerate) <= 0.001) {
      this.setData({ _eprate: false })
      return false;
    }
    wx.setStorageSync('khouseMCinmate', this.data.houseMCinmate)
    wx.setStorageSync('khousePurchasing', this.data.housePurchasing)
    wx.setStorageSync('khousepriceText', this.data.housepriceText)
    wx.setStorageSync('khousepricerate', this.data.housepricerate)
    wx.setStorageSync('khousepriceyear', this.data.housepriceyear)
    wx.setStorageSync('khouseMoyouth', this.data.houseMoyouth)
    wx.setStorageSync('khousecalculateType', this.data.housecalculateType) 
    wx.navigateTo({
      url: '/pages/Purchasing/houseprice/houseprice',
    })
  }
})